package com.wzdigit.wms.wms.controller.basic;

import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.framework.model.response.ErrorEntity;
import com.wzdigit.wms.common.utils.ValidatorUtil;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.client.request.basic.CreateVehicleRequest;
import com.wzdigit.wms.basic.client.request.basic.SearchVehicleRequest;
import com.wzdigit.wms.basic.client.request.basic.UpdateStatusVehicleRequest;
import com.wzdigit.wms.basic.client.request.basic.UpdateVehicleRequest;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.basic.ImportVehicleResponse;
import com.wzdigit.wms.basic.client.response.basic.VehicleApiResponse;
import com.wzdigit.wms.basic.client.response.basic.VehicleResponse;
import com.wzdigit.wms.wms.constant.WmsWareConstant;
import com.wzdigit.wms.wms.service.basic.VehicleService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 车辆 前端控制器
 * @author yangdongjie
 * @VehicleController.java
 * @2020年12月31日下午5:56:18
 */
@RestController
@Api(tags = "车辆")
@RequestMapping(value = WmsWareConstant.API_VERSION)
public class VehicleController {

	@Autowired
    private VehicleService vehicleService;

    private static final String REQUEST_MODEL = "/vehicle";
    
    
    /**
	 * 车辆（下拉框）列表（启用状态）
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 */
	@ApiOperation(value = "车辆（下拉框）列表（启用状态）",notes = "车辆（下拉框）列表（启用状态）")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/list")
    public List<VehicleApiResponse> list(
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return vehicleService.list();
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    /**
     * 查询车辆列表
     * @param dto
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @SuppressWarnings("rawtypes")
	@ApiOperation(value = "查询车辆列表",notes = "查询车辆列表")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/searchVehicle")
    public PagerInfo<List<VehicleResponse>> searchVehicle(
    		SearchVehicleRequest dto, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return vehicleService.searchVehicle(dto);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    
    /**
     * 查询车辆
     * @param vehicleId
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "查询车辆",notes = "查询车辆")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/getVehicle")
    public VehicleResponse getVehicle(
    		@ApiParam(value = "车辆ID", required = true) 
    		@RequestParam Integer vehicleId, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return vehicleService.getVehicle(vehicleId);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    
    /**
     * 创建车辆
     * @param dto
     * @param bindingResult
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "创建车辆",notes = "创建车辆")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PostMapping(value = REQUEST_MODEL + "/createVehicle")
    public VehicleResponse createVehicle(
    		@RequestBody @Valid CreateVehicleRequest dto, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
        	ValidatorUtil.validator(bindingResult);
            return vehicleService.createVehicle(dto, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    /**
     * 修改车辆
     * @param dto
     * @param bindingResult
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "修改车辆",notes = "修改车辆")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PutMapping(value = REQUEST_MODEL + "/updateVehicle")
    public VehicleResponse updateVehicle(
    		@RequestBody @Valid UpdateVehicleRequest dto, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
        	ValidatorUtil.validator(bindingResult);
            return vehicleService.updateVehicle(dto, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    /**
     * 修改车辆状态
     * @param dto
     * @param bindingResult
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "修改车辆状态",notes = "修改车辆状态")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PutMapping(value = REQUEST_MODEL + "/updateStatus")
    public <T> Result<T> updateStatus(
    		@RequestBody @Valid UpdateStatusVehicleRequest dto, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
        	ValidatorUtil.validator(bindingResult);
            return vehicleService.updateStatus(dto, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    /**
     * 导入《车辆信息》
     * @param file
     * @param servletRequest
     * @param servletResponse
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "导入《车辆信息》", notes = "导入《车辆信息》")
    @ApiResponses({@ApiResponse(code = 500, message = "系统异常", response = ErrorEntity.class)})
    @PostMapping(value = REQUEST_MODEL + "/importVehicle")
    public ImportVehicleResponse importVehicle(MultipartFile file, 
			HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws Exception {
    	try {
			return vehicleService.importVehicle(file, servletRequest, servletResponse);
    	} catch (RestStatusException e) {
        	e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
	}
}
